'use strict';
myApp.controller('dashboardCtl',function($scope, $serDashboard,$serAdmin,$localStorage,toaster){

    $scope.vm={
        admin:{},
        shop:{},
        data:{},
        flag:$localStorage.cartImg,
        flag2:$localStorage.hotImg,
        d3:{},
        d_01:[],
        d_03:[],
        refurbish:function () {
            $serDashboard.getDashboard({
                token : $localStorage.userInfo&&$localStorage.userInfo.token,
            }).then(function(res){
                if(res.code ==1){
                    $scope.vm.data = res.data;
                    $scope.vm.d3 = res.data.chart.order_percent;
                    angular.forEach($scope.vm.d3,function (data,index) {
                        $scope.d3[index].label = data.status_name
                        $scope.d3[index].data = Number(data.total)
                    })
                    $localStorage.d3=$scope.d3

                    $scope.vm.d_01 = res.data.chart.order_30;
                    angular.forEach($scope.vm.d_01,function (data,index) {
                        $scope.d_0.push(Number(index),Number(data.z))
                        $scope.d_01.push($scope.d_0)
                        $scope.d_0=[]
                        $scope.d_0.push(Number(index),Number(data.y))
                        $scope.d_02.push($scope.d_0)
                        $scope.d_0=[]
                    })
                    $localStorage.d_01=$scope.d_01
                    $localStorage.d_02=$scope.d_02

                    $scope.vm.d_03 = res.data.chart.user_30;
                    angular.forEach($scope.vm.d_03,function (data,index) {
                        $scope.d0_3[index].push(Number(data.y))
                    })
                    $localStorage.d0_3=$scope.d0_3
                    toaster.success('提示','刷新成功');
                }else {
                    toaster.success('提示',res.message);
                }
            })
        },
        changeImg:function () {
            $localStorage.cartImg = $scope.vm.flag;
        },
        changeImg2:function () {
            $localStorage.hotImg = $scope.vm.flag2;
        }

    };
    $scope.d3 = [
        {},
        {},
        {},
        {},
        {}
    ];
    $scope.d_01 = [
    ];
    $scope.d_01x = [
    ];
    $scope.d_02 = [
    ];
    $scope.d_0 = []
    $scope.d0_3 = [ [0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15]
                   ,[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29]];
    $scope.d3_1=$localStorage.d3

    $scope.d_01_1=$localStorage.d_01
    $scope.d_02_1=$localStorage.d_02

    $scope.d0_3_1=$localStorage.d0_3
    var int = function () {
        angular.forEach($scope.vm.d3,function (data,index) {
            $scope.d3[index].label = data.status_name
            $scope.d3[index].data = Number(data.total)
        })
        $localStorage.d3=$scope.d3
        angular.forEach($scope.vm.d_01,function (data,index) {
            $scope.d_1=[]
            $scope.d_1.push(Number(index),Number(data.z))
            $scope.d_01.push($scope.d_1)
            $scope.d_01x.push([index,data.x.match(/-(\d+-\d+)/)[1]]);
            $scope.d_0=[]
            $scope.d_0.push(Number(index),Number(data.y))
            $scope.d_02.push($scope.d_0)
            $scope.d_0=[]
        })
        $localStorage.d_01=$scope.d_01
        $localStorage.d_02=$scope.d_02
        angular.forEach($scope.vm.d_03,function (data,index) {
            $scope.d0_3[index].push(Number(data.y))
        })
        $localStorage.d0_3=$scope.d0_3
    }
    var init = function () {
        $serDashboard.getDashboard({
            token : $localStorage.userInfo&&$localStorage.userInfo.token,
        }).then(function(res){
            if(res.code ==1){
                $scope.vm.data = res.data;
                $scope.vm.d3 = res.data.chart.order_percent;
                $scope.vm.d_01 = res.data.chart.order_30;
                $scope.vm.d_03 = res.data.chart.user_30;
                int()
            }
        })
    }

    init();
});


